package com.nit.lab.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.system.KaptchaDTO;
import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.service.system.AccountService;
import com.nit.lab.utils.HttpRequestReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(value = "/api/account")
public class AccountController {

    @Resource
    private AccountService accountService;


    private final Logger logger = LoggerFactory.getLogger(AccountController.class);

    /**
     * @Description: 用户账号登录验证
     * @Author: SN
     * @Date: 2020/02/31 11:02
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public JSONObject loginCheck(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String loginName = jsonRequest.getString("loginName");
            String loginPwd = jsonRequest.getString("loginPwd");
//            String randomId = jsonRequest.getString("randomId");
//            String authCode = jsonRequest.getString("authCode");
            jsonObject = accountService.loginCheck(loginName, loginPwd);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return jsonObject;
    }

    /**
     * @Description: 获取当前用户的信息
     * @Author: SN
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public OpResultDTO getInfo(@RequestHeader(name = "Access-Token") String token) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage("success");
            opResult.setResult(accountService.getInfo(token));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 生成验证码
     * @Author: SN
     * @Date: 2020/11/31 11:02
     */
    @RequestMapping(path = "/kaptcha", method = RequestMethod.GET)
    public OpResultDTO getKaptcha(HttpServletResponse response) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            KaptchaDTO kaptchaDTO = accountService.getKaptcha();
            opResult.setMessage("success");
            opResult.setResult(kaptchaDTO);
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
}
